package com.test.ws.s1.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.test.ws.s1.bean.Acl_user;
import com.test.ws.s1.bean.Supplier;
import com.test.ws.s1.bean.Userqt;
import com.test.ws.s1.service.Acl_userService;
import com.test.ws.s1.service.SupplierService;
import com.test.ws.s1.utils.Res;
import com.test.ws.s1.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.UUID;

@Controller
public class Acl_userController {
    @Autowired
    private Acl_userService acl_userService;
    @Autowired
    private SupplierService supplierService;

    @RequestMapping("/acl_userCha")
    public String selectAcl_user(Acl_user acl_user, HttpServletRequest request,
                                 @RequestParam(defaultValue = "1") int pageNum,
                                 @RequestParam(defaultValue = "3") int pageSize) {

        Acl_user userqt = new Acl_user();
        PageHelper pageHelper = new PageHelper();
        pageHelper.startPage(pageNum, pageSize);
        ArrayList<Acl_user> acl_users = acl_userService.selectAcl_user(acl_user);
        System.out.println("============================" + acl_users);
        PageInfo pageInfo = new PageInfo(acl_users);

        request.setAttribute("acl_users", acl_users);
        request.setAttribute("pageInfo", pageInfo);
        request.setAttribute("sp", userqt);
        return "houtai/htyu";
    }

    /// 回显
    @RequestMapping("/selectOneAcl_user")
    public String selectOneAcl_user(Acl_user acluser, HttpServletRequest request) {
        String id = request.getParameter("id");
        Acl_user acl_user = acl_userService.selectOneAcl_user(Integer.parseInt(id));
        request.setAttribute("ac", acl_user);
        return "houtai/htyuXiuGai";
    }

    /// 修改
    @RequestMapping("/acl_userGai")
    public String updateUsers(MultipartFile image, Acl_user user, Model model) throws SQLException, IOException {
        if (image != null && image.getSize() > 0) {
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            String houZhui = image.getOriginalFilename().split("\\.")[1];
            ///新增加的头像路径
            String path = Res.fileDir + uuid + "." + houZhui;
            image.transferTo(new File(path));
            user.setAcl_tx(uuid + "." + houZhui);
            /// 获取旧头像
            Acl_user aclUser = acl_userService.selectOneAcl_user(user.getUser_id());
            String tx = aclUser.getAcl_tx();

            if (tx != null && !tx.equals(user.getAcl_tx())) {
                File file = new File(Res.fileDir + tx);
                if (file.exists()) {
                    file.delete();
                }
            }
            user.setPassword(SecurityUtils.encrypt(user.getPassword()));
            int i = acl_userService.updateAcl_user(user);
            if (i > 0) {
                return "redirect:/acl_userCha";
            }
        }
        return "redirect:/acl_userCha";
    }

    /// 进入增加页面
    @RequestMapping("/acl_useraddym")
    public String acl_useraddym(Acl_user users,HttpServletRequest request,MultipartFile image ,Supplier supplier) throws IOException {
        ArrayList<Supplier> suppliers1 = supplierService.selectSupplier(supplier);
        request.setAttribute("supplierCha",suppliers1);
       return "houtai/htyuZj";
    }



    /// 增加
    @RequestMapping("/acl_userAdd")
    public String insertAcl_user(Acl_user users,HttpServletRequest request,MultipartFile image) throws IOException {
        /// 1. image.getSize() > 0 检查上传的文件是否非空。
        if (image.getSize()>0){
            /// 2. 使用UUID.randomUUID()生成一个全局唯一标识符，并通过replaceAll("-", "")去掉其中的-符号。
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");

            /// 3. 提取文件后缀名：image.getOriginalFilename().split("\\.")[1]，假设文件名为example.jpg，则提取到后缀jpg。
            String houZhui = image.getOriginalFilename().split("\\.")[1];

            image.transferTo(new File(Res.fileDir + uuid+"."+houZhui));

            /// 4. 将UUID与原始文件名拼接起来，生成新的文件名，并调用users.setTx()将其保存到Users对象中。
            users.setAcl_tx(uuid+"."+houZhui);

        }

        /// 5. 调用userSever.insertUsers()方法将Users对象插入到数据库中。
        users.setPassword(SecurityUtils.encrypt(users.getPassword()));
        int i = acl_userService.insertAcl_user(users);

        /// 6. 如果插入成功，重定向到/users/selectUsers，否则返回"insert"视图。
        if (i>0){
            return "redirect:/acl_userCha";
        }
        return "redirect:/acl_userCha";
    }

    /// 删除
    @RequestMapping("/acl_userDel")
    public String deleteAcl_user(Integer user_id,HttpServletRequest request) {
        String id = request.getParameter("id");
        int i = acl_userService.deleteAcl_user(Integer.parseInt(id));
        if (i > 0) {
            return "redirect:/acl_userCha";
        }
        return "redirect:/acl_userCha";
    }

}